﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Linq.Expressions;

public partial class Manage_ListQuestions : System.Web.UI.Page
{
    protected PagedDataSource pds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aQ in db.Question
                select aQ;

        #region 检索条件
        string search = SearchConditionHidden.Value.Trim();
        if (search != "")
        {
            Expression<Func<Question, bool>> lmbda = PredicateBuilder<Question>.CreatePredicate(new string[] { 
                "Name",
                "Course", 
                "Kind", 
                "Subject",
                "Title"}, search, "aQ");
            q = q.Where(lmbda);
        }
        #endregion

        var p = q.OrderByDescending(aQ => aQ.Time).Select(aQ => new
                {
                    ID = aQ.ID,
                    Name = aQ.Name,
                    Course = aQ.Country,
                    Kind = aQ.Kind,
                    Subject = aQ.Subject,
                    Title = aQ.Title,
                    Verified = aQ.Verified,
                    Verifier = aQ.Manager.Name,
                    Time = aQ.Time
                });

        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = Toolkit.PageSize;
        pds.DataSource = p.ToList();


        #region 获取当前要显示的页
        int pageIndex = 0;
        pageIndex = Convert.ToInt32(CurrentPage.Value) - 1;
        if (pageIndex >= pds.PageCount)
        {
            pageIndex = pds.PageCount - 1;
        }
        if (pageIndex <= 0)
        {
            pageIndex = 0;
        }
        #endregion

        pds.CurrentPageIndex = pageIndex;

        TopicsView.DataSource = pds;
        TopicsView.DataBind();

        CurrentPage.Value = Convert.ToString(pageIndex + 1);
        SearchCondition.Text = SearchConditionHidden.Value.Trim();
    }
    protected void ChangePage(object sender, CommandEventArgs e)
    {
        string kind = Convert.ToString(e.CommandArgument);
        int p = Convert.ToInt32(CurrentPage.Value);

        if (kind == "F")
        {
            CurrentPage.Value = "1";
        }
        else if (kind == "P")
        {
            p--;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "N")
        {
            p++;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "L")
        {
            CurrentPage.Value = Convert.ToString(Int32.MaxValue);
        }
        else if (kind == "G")
        {
            TextBox gtp;
            foreach (Control c in TopicsView.Controls)
            {
                gtp = c.FindControl("GotoPage") as TextBox;
                if (gtp != null)
                {
                    try
                    {
                        CurrentPage.Value = Convert.ToString(Convert.ToInt32(gtp.Text.Trim()));
                    }
                    catch
                    {
                        //CurrentPage.Value = "1";
                    }

                    break;
                }
            }
        }

        BindData();
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        SearchConditionHidden.Value = SearchCondition.Text.Trim();
        BindData();
    }
    protected void Delete_Click(object sender, EventArgs e)
    {
        string tids = Request.Params["chkID"];

        if (tids != null)
        {
            string[] ids = tids.Split(',');
            CmsDataContext db = new CmsDataContext();

            var q = from aQ in db.Question
                    where ids.Contains(aQ.ID)
                    select aQ;

            foreach (Question qs in q)
            {
                db.Answer.DeleteAllOnSubmit(qs.Answer);
            }

            db.Question.DeleteAllOnSubmit(q);


            //提交数据库更新
            db.SubmitChanges();

            MessageBox.Message = "已经删除所选问题。";
        }

        //重新绑定数据
        BindData();
    }
}